package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.MessageProducer;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import utils.Encrypt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

 @Autowired
 private UserService userService;

 @Autowired
 private DeptService deptService;

 @Autowired
 private RoleService roleService;

 @Autowired
 private MessageProducer producer;

 /**
  * 根据登录人企业id查询用户列表（分页）
  * system/user/list.do
  */
 @RequestMapping("/list")
public String list(
 @RequestParam(defaultValue = "1") int page,
 @RequestParam(defaultValue = "10") int size
 ) {
  String companyId = getLoginCompanyId();

  PageInfo info = userService.findAll(companyId, page, size);

  request.setAttribute("page",info);

  return "system/user/user-list";
 }

 /**
  * 进入保存页面（查询所有部门）
  * system/user/toAdd.do
  */
 @RequestMapping("/toAdd")
 public String toAdd() {
  //查询所有部门，为了构造下拉框，不分页
  String companyId = getLoginCompanyId();
  List list = deptService.findAll(companyId);
  //部门列表存入request域
  request.setAttribute("deptList",list);
  //跳转页面
  return "system/user/user-add";
 }

 /**
  * 保存或更新用户
  * system/user/edit.do
  */
 @RequestMapping("/edit")
 public String edit(User user) {
  //设置企业参数
  user.setCompanyId(getLoginCompanyId());
  user.setCompanyName(getLoginCompanyName());
  //判断是否存在id
  if (StringUtils.isEmpty(user.getId())) {
   String password = user.getPassword();
   user.setPassword(Encrypt.md5(password, user.getEmail()));
   //不存在id，调用service保存
   userService.save(user);
   //发送邮件
   String to = user.getEmail();
   String subject = "欢迎使用Saas-Export系统";
   String content = "尊敬的用户您好,欢迎使用Saas-Export系统。您的访问地址是 http://127.0.0.1:8088 , 登录用户名："+
    user.getEmail()+", 登录密码" + password;

   Map<String, String> map = new HashMap<>();
   map.put("to", to);
   map.put("subject", subject);
   map.put("content", content);

   producer.send("user.insert", map);
  } else {
   //存在id，调用service更新
   userService.update(user);
  }
  return "redirect:/system/user/list.do";
 }
 /**
  * 进入修改页面
  */
 @RequestMapping("/toUpdate")
 public String toUpdate(String id) {
  //调用service查询id
  User user = userService.findById(id);
  //存入request域
  request.setAttribute("user",user);
  //查看所有部门
  String companyId = getLoginCompanyId();
  List<Dept> list = deptService.findAll(companyId);
  //将部门存入request域
  request.setAttribute("deptList",list);
  //跳转更新页面
  return "system/user/user-update";
 }

 /**
  * 根据id删除
  */
 @RequestMapping("/delete")
 public String delete(String id) {
  userService.delete(id);
  return "redirect:/system/user/list.do";
 }

 /**
  * 进入角色分配页面
  * /system/user/roleList.do
  */
 @RequestMapping("/roleList")
 public String roleList(String id) {
  //根据id查询用户
  User user = userService.findById(id);
  //存入request域中
  request.setAttribute("user",user);
  //查询所有角色
  List<Role> roleList = roleService.findAll(getLoginCompanyId());
  //存入request域中
  request.setAttribute("roleList",roleList);
  //根据用户id，查询此用户所有角色id
  List<String> roles = userService.findRolesByUserId(id);
  String userRoleStr = "";
  for (String roleId : roles) {
   userRoleStr += roleId + " ";
  }
  //存入request域中
  request.setAttribute("userRoleStr",userRoleStr);
  //跳转页面
  return "system/user/user-role";
 }

 /**
  * 实现对用户分配角色
  * /system/user/changeRole.do
  */
 @RequestMapping("/changeRole")
 public String changeRole(String userid, String[] roleIds) {
  //调用service
  userService.changeRole(userid,roleIds);
  //重定向
  return "redirect:/system/user/list.do";
 }
}
